import ElevatorP.Elevator;
import ElevatorP.ElevatorI;
import PassengerP.Passenger;

import java.util.*;

/**
 * Created by Connor Madden.
 * User: Connor Madden
 * Date: 15/09/11
 * Time: 9:43 AM
 */
public class Simulation {
    private boolean running;
    private TreeMap<Integer, Floor> floors;
    private LinkedList<ElevatorI> elevators;
    private static final float ELEV_START_POSITION = 0.0f;

    /**
     * @param numElevators  number of elevators
     * @param numFloors     number of floors
     * @param elevatorSpeed speed of elevators (floors per second)
     */
    public Simulation(int numElevators, int numFloors, float elevatorSpeed) {
        running = false;

        floors = new TreeMap<Integer, Floor>();
        for (Integer i = 0; i < numFloors; i++) {
            floors.put(i, new Floor(i, "Floor " + i));
        }

        elevators = new LinkedList<ElevatorI>();
        for (int i = 0; i < numElevators; i++) {
            elevators.add(new Elevator(i, elevatorSpeed, ELEV_START_POSITION));
        }
    }

    public boolean newArrival(int destination, int arrivalPoint) {
        Floor arrivalFlr = floors.get(arrivalPoint);
        Floor destinationFlr = floors.get(destination);
        //DEBUG
        if (destination == arrivalPoint) {
            System.out.println("");
            Integer[] floorDestinations = new Integer[getNumFloors()];
            for (int floorNum : floors.keySet()) {
                Floor floor = floors.get(floorNum);
                for (Passenger passenger : floor.getArrivals()) {
                    if (floorDestinations[passenger.getDestination()] == null) {
                        floorDestinations[passenger.getDestination()] = 1;
                    } else {
                        floorDestinations[passenger.getDestination()] = floorDestinations[passenger.getDestination()]++;
                    }
                }
            }
            System.out.println("");
            for (int i = 0; i < floorDestinations.length; i++) {
                Integer floorDestination = floorDestinations[i];
                System.out.println("floor " + i);
                System.out.println(floorDestination);
            }
            // DEBUG end
        }

        if (arrivalFlr == null || destinationFlr == null || destination == arrivalPoint) {
            return false;
        } else {
            Passenger passenger = new Passenger("name", destination, arrivalPoint);
            arrivalFlr.addArrival(passenger);
            awake();
            return true;
        }
    }

    private boolean awake() {
        LinkedList<ElevatorI> asleepElevs = new LinkedList<ElevatorI>();
        for (ElevatorI elevatorI : elevators) {
            if (elevatorI.getState() == Elevator.State.STOPPED) {
                asleepElevs.add(elevatorI);
            }
        }
        if (asleepElevs.size() == 0) {
            return false;
        }
        for (ElevatorI elevatorI : asleepElevs) {
            getMove(elevatorI);
        }
        return true;
    }

    private boolean getMove(final ElevatorI elevatorI) {

        for (int floorNum : floors.keySet()) {    // for each floor
            Floor floor = floors.get(floorNum);
            if (floor.getArrivals().size() > 0) {   // if floor has arrivals
                Passenger passenger = floor.getArrivals().getFirst();
                elevatorI.addPassenger(passenger);
                floor.removeArrival(passenger);
                if (elevatorI.countObservers() == 0) {
                    Observer observer = new Observer() {
                        public void update(Observable o, Object arg) {
                            // if finishes move
                            System.out.println(((Object[]) arg)[0] + ", " + ((Object[]) arg)[1]);
                            if (((Object[]) arg)[0] == Elevator.State.STOPPED && (Integer) ((Object[]) arg)[1] == 1) {
                                awake();
                            }
                        }
                    };
                    elevatorI.addObserver(observer);
                }
                moveElev(elevatorI, passenger.getDestination());
                return true;
            }
        }
        return false;
    }

    private boolean moveElev(ElevatorI elevatorI, float destination) {
        return elevatorI.move(destination);
    }

    public boolean run() {
        if (!running) {
            running = true;
            for (ElevatorI elevator : elevators) {
                elevator.unPause();
            }
            return true;
        } else {
            return false;
        }
    }

    public boolean stop() {
        if (running) {
            running = false;
            for (ElevatorI elevator : elevators) {
                elevator.pause();
            }
            return true;
        } else {
            return false;
        }
    }

    public boolean isRunning() {
        return running;
    }

    public LinkedList<Floor> getFloors() {
        return new LinkedList<Floor>(floors.values());
    }

    public LinkedList<ElevatorI> getElevators() {
        return elevators;
    }

    public int getNumFloors() {
        return floors.size();
    }

    public int getNumElevators() {
        return elevators.size();
    }
}
